package me.rainsay;

public class PathUtils {


    public static PathNode splitNode(String path) {
        String[] paths = path.split("\\.");
        PathNode firstNode = new PathNode("", PathType.ROOT);
        PathNode node = firstNode;
        for(String p : paths) {

            int lqi = p.indexOf("[");
            int rqi = p.indexOf("]");
            if(lqi >= 0 && rqi > 0 && rqi > lqi) {
                String tp = p.substring(0, lqi);
                PathNode inode = new PathNode(tp, PathType.STRING);
                node.setNext(inode);
                node = inode;
                do {
                    String index = p.substring(lqi + 1, rqi);
                    inode = new PathNode(index, PathType.INDEX);
                    node.setNext(inode);
                    node = inode;
                    lqi = p.indexOf("[", lqi+1);
                    rqi = p.indexOf("]", rqi + 1);
                }while(lqi >=0 && rqi > 0 && rqi > lqi);
            }else {
                PathNode inode = new PathNode(p, PathType.INDEX);
                node.setNext(inode);
                node = inode;
            }
        }
        return firstNode;
    }
}
